package com.astrob.activitys;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.astrob.adapters.FavwenluAdapter;
import com.astrob.model.FavoriteData;
import com.astrob.model.FavoriteFileHandle;
import com.astrob.model.HistoryData;
import com.astrob.model.Msg;
import com.astrob.model.RoutePlanDataHandle;
import com.astrob.nhcmap.R;
import com.astrob.util.Utils;

public class NewFavActivity extends Activity implements
		FavwenluAdapter.PoiItemListener {

	private ListView mListView;
	private FavwenluAdapter mFavAdapter;


	private int mSelectType; // 0: 收藏 1：问路
	private int mSelectLvIndex = -1;

	private int mFromRouteMgrType = -1;
	private int mFromRouteMgrDestIndex = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_newfav);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText(R.string.favhistroy_title_text);

		mListView = (ListView) findViewById(R.id.listview);
		mListView.setVerticalScrollBarEnabled(false);

		mSelectType = 0;
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			mFromRouteMgrType = extras.getInt("TypeFlag", -1);
			mFromRouteMgrDestIndex = extras.getInt("DestIndex", -1);
		}

		updateList();
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(android.os.Message msg) {
			onClickedListViewItem(msg.what - Msg.CLICKLISTVIEWITEM0);
		}
	};

	private void onClickedListViewItem(int nIndex) {
		if (nIndex < 0) {
			return;
		}
		mSelectLvIndex = nIndex;

		if (mFromRouteMgrType != -1) {
			ChangeRouteMgrData();
		} else {
			showonmap();
		}
	}

	private void updateList() {
		{
			if (null == mFavAdapter) {
				mFavAdapter = new FavwenluAdapter(this);
				mFavAdapter.setListener(this);
			} else {
				if (mSelectType == 0) {
					mFavAdapter.updataDatas(true);
				} else {
					mFavAdapter.updataDatas(false);
				}
			}
			mListView.setAdapter(mFavAdapter);
			mFavAdapter.notifyDataSetChanged();
			mListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> parent, View view,
										int position, long id) {
					if (Utils.isFastDoubleClick())
						return;
					mHandler.sendEmptyMessage(Msg.CLICKLISTVIEWITEM0 + position);
				}
			});
		}

		if (mSelectLvIndex >= 0) {
			mListView.setSelection(mSelectLvIndex);
		}
	}

	public void onBack(View v) {
		finish();
	}

	private void showonmap() {
		double dlon, dlat;
		String name;
		if (mSelectType == 0) {
			FavoriteFileHandle.getInstance().editFav = mFavAdapter.getData(mSelectLvIndex);
			FavoriteData data = mFavAdapter.getData(mSelectLvIndex);
			dlon = data.lon;
			dlat = data.lat;
			name = data.name;

			Intent intent = new Intent(this, NaviActivity.class);
			intent.putExtra("mapcenterlon", dlon); //
			intent.putExtra("mapcenterlat", dlat);
			intent.putExtra("poiname", name);
			intent.putExtra("showOnmap", true);
			intent.putExtra("fromfav", true);
			startActivityForResult(intent, Msg.REQUEST_SHOW_ON_MAP);

		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == Msg.REQUEST_SHOW_ON_MAP) {
			updateList();
		}
		
		if (resultCode == Msg.EDITED_CONTENT) {
			FavoriteFileHandle.getInstance().editFav.name = EditImeActivity.getEditResult();
			FavoriteFileHandle.getInstance().save();
		}
		updateList();
		
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onEditinfo(int pos) {
		mSelectLvIndex = pos;
		FavoriteFileHandle.getInstance().editFav = mFavAdapter.getData(pos);
		String strEdit = mFavAdapter.getData(pos).strExInfo;
		Intent intent = new Intent(this, EditImeActivity.class);
		intent.putExtra("editstring", strEdit);
		intent.putExtra("title", getString(R.string.edit_exinfo_title_text));
		startActivityForResult(intent, 0);
	}
	
	@Override
	public void onShowFavPic(int pos) {
		// 修改名称
		mSelectLvIndex = pos;
		FavoriteFileHandle.getInstance().editFav = mFavAdapter.getData(pos);
		String strEditName = mFavAdapter.getData(pos).name;
		Intent intent = new Intent(this, EditImeActivity.class);
		intent.putExtra("editstring", strEditName);
		intent.putExtra("title", "修改名称");
		startActivityForResult(intent, 0);
	}

	@Override
	public void onDelete(int pos) {
		mSelectLvIndex = pos;

		new AlertDialog.Builder(this)
				.setTitle(getString(R.string.dlg_title))
				.setMessage("确定删除？")
				.setPositiveButton(getString(R.string.yes),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								delItem();
							}
						}).setNegativeButton(getString(R.string.cancel), null)
				.show();
	}

	public void delItem() {
		if (mSelectType == 0) {
			FavoriteFileHandle.getInstance().delete(
					mFavAdapter.getData(mSelectLvIndex));
			FavoriteFileHandle.getInstance().save();
		}
		updateList();
	}

	private void ChangeRouteMgrData() {
		HistoryData newdata = new HistoryData();
		if (mSelectType == 0) {
			FavoriteData data = mFavAdapter.getData(mSelectLvIndex);
			newdata.lon = data.lon;
			newdata.lat = data.lat;
			newdata.name = data.name;
			newdata.address = data.address;
		}

		if (1 == mFromRouteMgrType) {
			RoutePlanDataHandle.getInstance().setStart(newdata);
		} else if (2 == mFromRouteMgrType && mFromRouteMgrDestIndex >= 0) {
			RoutePlanDataHandle.getInstance().changeDest(newdata,
					mFromRouteMgrDestIndex);
		} else if (3 == mFromRouteMgrType) {
			RoutePlanDataHandle.getInstance().addDest(newdata);
		}

		setResult(Msg.CHANGE_ROUTEMGR);
		finish();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}

}
